package com.yz.jdd.module.main.presenter;

import android.content.Context;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.yz.jdd.base.entity.Response_Base;
import com.yz.jdd.base.utils.LogUtils;
import com.yz.jdd.base.utils.StringUtil;
import com.yz.jdd.module.main.adapter.ShopsAdapter;
import com.yz.jdd.module.main.entity.OrderBean;
import com.yz.jdd.module.main.entity.ShopListItem;
import com.yz.jdd.network.MySubscriber;
import com.yz.jdd.network.NetWorkRequset;
import com.yz.jdd.network.request.Request_SetShopPay;
import com.yz.jdd.network.response.Response_OrderList;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;


public class ShopListPresenter implements ShopListContract.Presenter {


    private Context context;
    private int layoutId;
    private ShopListContract.View orderView;
    private ShopsAdapter orderListAdapter;

    public String pages = "1";
    public String pageSize = "10";

    private int groupP = 0;
    private int childP = 0;

    private boolean isSetAct;
    String from;

    private List<ShopListItem.ResultObjectBean.PageDataBean> pageDataBeans = new ArrayList<>();

    public ShopListPresenter(Context context, int layoutId, ShopListContract.View orderView,boolean isSetAct,String from) {
        this.orderView = orderView;
        orderView.setPresenter(this);
        this.context = context;
        this.layoutId = layoutId;
        this.isSetAct = isSetAct;
        this.from=from;

    }

    @Override
    public ShopsAdapter getAdapter() {
        if (orderListAdapter == null) {
            synchronized (this) {
                if (pageDataBeans == null) {
                    pageDataBeans = new ArrayList<>();
                }
                orderListAdapter = new ShopsAdapter(context, pageDataBeans,isSetAct,from);  //orderExtendableListViewAdapter
                orderView.setAdapter();

            }
        }
        return orderListAdapter;
    }

    @Override
    public void chooseAllShop() {
        try {
            pageDataBeans.get(groupP).getShops().get(childP).setCheck(false);
            getAdapter().notifyDataSetChanged();
            orderView.selectedAllShop();
        } catch (Exception e) {

        }
    }

    @Override
    public void chooseOtherShop(int par, int child) {
        try {
            // 产品说,不按需求说明做 ,未审核通过的也可以点击...
            //   int status = (int) pageDataBeans.get(par).getShops().get(child).getAuditStatus();
            //    if (status == 5 || status == 6) {
            pageDataBeans.get(groupP).getShops().get(childP).setCheck(false);
            pageDataBeans.get(par).getShops().get(child).setCheck(true);
            groupP = par;
            childP = child;
            orderView.selectedOtherShop(pageDataBeans.get(par).getShops().get(child).getShopId(), pageDataBeans.get(par).getShops().get(child).getShopName(), true);
            getAdapter().notifyDataSetChanged();
            //     }
        } catch (Exception e) {

        }
    }

    /**
     * 调用设置  电子券核销门店
     * @param par
     * @param child
     */
    @Override
    public void chooseEvoucherShop(int par, int child) {

        pageDataBeans.get(par).getShops().get(child).setCheck(true);
        getAdapter().notifyDataSetChanged();
        final ShopListItem.ResultObjectBean.PageDataBean.ShopsBean shopsBean=pageDataBeans.get(par).getShops().get(child);



        Request_SetShopPay request_setShopPay =new Request_SetShopPay();
        request_setShopPay.setType("app_market");
        request_setShopPay.setShopId(shopsBean.getShopId());

        NetWorkRequset.saveAndUpdateCommonConfig(request_setShopPay, new MySubscriber() {
            @Override
            public void onSuccess(Response_Base response_baseModel) {
                orderView.selectedShop_ScanEvoucherActivity(shopsBean);
            }

            @Override
            public void onNeedLogin(String msg) {

            }

            @Override
            public void onFail(Response_Base response_baseModel) {
                orderView.showToastDialog(!TextUtils.isEmpty(response_baseModel.getMsg())?response_baseModel.getMsg():"加载失败");
            }
        });
    }

    @Override
    public void chooseShop_SetActivity(int par, int child) {
        pageDataBeans.get(par).getShops().get(child).setCheck(true);
        getAdapter().notifyDataSetChanged();
        orderView.selectedShop_SetActivity(pageDataBeans.get(par).getShops().get(child));
    }

    /**
     * 获取核销门店
     * @param shopId
     */
    @Override
    public void doGetEvoucherShopList(final String shopId) {
        NetWorkRequset.getShopSelectForMarket(new MySubscriber() {
            @Override
            public void onSuccess(Response_Base response_baseModel) {
                if (response_baseModel.getResultObject() != null) {
                    //   LogUtils.iLong(response_baseModel.getResultObject().toString());
                    String json = new Gson().toJson(response_baseModel);
                    //      LogUtils.iLong("返回json ; " + json);
                    ShopListItem shopListItem = new Gson().fromJson(json, ShopListItem.class);
                    pageDataBeans = shopListItem.getResultObject().getPageData();
                    if (!StringUtil.isBlank(shopId)) {
                        for (int i = 0; i < pageDataBeans.size(); i++) {
                            for (int k = 0; k < pageDataBeans.get(i).getShops().size(); k++) {
                                if (pageDataBeans.get(i).getShops().get(k).getShopId().equals(shopId)) {
                                    pageDataBeans.get(i).getShops().get(k).setCheck(true);
                                    orderView.selectedOtherShop(shopId, pageDataBeans.get(i).getShops().get(k).getShopName(), false);
                                    groupP = i;
                                    childP = k;
                                }
                            }
                        }
                    }
                    getAdapter().notifyDataSetChanged();
                    orderView.expandGroup(pageDataBeans.size());
                }
            }

            @Override
            public void onNeedLogin(String msg) {

            }

            @Override
            public void onFail(Response_Base response_baseModel) {
                orderView.showToastDialog(response_baseModel.getMsg());
            }

        });

    }

    @Override
    public void doSearch(String s) {
        groupP = 0;
        childP = 0;

        if (!StringUtil.isBlank(s)) {
            for (int i = 0; i < pageDataBeans.size(); i++) {
                for (int k = 0; k < pageDataBeans.get(i).getShops().size(); k++) {
                    pageDataBeans.get(i).getShops().get(k).setCheck(false);
                    if (!pageDataBeans.get(i).getShops().get(k).getShopName().toLowerCase().contains(s.toLowerCase())) {
                        //     LogUtils.i(pageDataBeans.get(i).getShops().get(k).getShopName());
                        pageDataBeans.get(i).getShops().get(k).setShow(true);
                    } else {
                        pageDataBeans.get(i).getShops().get(k).setShow(false);
                    }
                }
            }
        } else {
            for (int i = 0; i < pageDataBeans.size(); i++) {
                for (int k = 0; k < pageDataBeans.get(i).getShops().size(); k++) {
                    pageDataBeans.get(i).getShops().get(k).setShow(false);
                }
            }
        }
        getAdapter().notifyDataSetChanged();
    }

    @Override
    public void doGetShopList(final String shopId) {

        NetWorkRequset.doShoplist(new MySubscriber() {
            @Override
            public void onSuccess(Response_Base response_baseModel) {
                if (response_baseModel.getResultObject() != null) {
                 //   LogUtils.iLong(response_baseModel.getResultObject().toString());
                    String json = new Gson().toJson(response_baseModel);
              //      LogUtils.iLong("返回json ; " + json);
                    ShopListItem shopListItem = new Gson().fromJson(json, ShopListItem.class);
                    pageDataBeans = shopListItem.getResultObject().getPageData();
                    if (!StringUtil.isBlank(shopId)) {
                        for (int i = 0; i < pageDataBeans.size(); i++) {
                            for (int k = 0; k < pageDataBeans.get(i).getShops().size(); k++) {
                                if (pageDataBeans.get(i).getShops().get(k).getShopId().equals(shopId)) {
                                    pageDataBeans.get(i).getShops().get(k).setCheck(true);
                                    orderView.selectedOtherShop(shopId, pageDataBeans.get(i).getShops().get(k).getShopName(), false);
                                    groupP = i;
                                    childP = k;
                                }
                            }
                        }
                    }
                    getAdapter().notifyDataSetChanged();
                    orderView.expandGroup(pageDataBeans.size());
                }
            }

            @Override
            public void onNeedLogin(String msg) {

            }

            @Override
            public void onFail(Response_Base response_baseModel) {
                orderView.showToastDialog(response_baseModel.getMsg());
            }

        });


    }

    @Override
    public void doGetPayShopList(final String shopId) {

        NetWorkRequset.getShopSelectForPay(new MySubscriber() {
            @Override
            public void onSuccess(Response_Base response_baseModel) {
                if (response_baseModel.getResultObject() != null) {
                    //   LogUtils.iLong(response_baseModel.getResultObject().toString());
                    String json = new Gson().toJson(response_baseModel);
                    //      LogUtils.iLong("返回json ; " + json);
                    ShopListItem shopListItem = new Gson().fromJson(json, ShopListItem.class);
                    pageDataBeans = shopListItem.getResultObject().getPageData();
                    if (!StringUtil.isBlank(shopId)) {
                        for (int i = 0; i < pageDataBeans.size(); i++) {
                            for (int k = 0; k < pageDataBeans.get(i).getShops().size(); k++) {
                                if (pageDataBeans.get(i).getShops().get(k).getShopId().equals(shopId)) {
                                    pageDataBeans.get(i).getShops().get(k).setCheck(true);
                                    orderView.selectedOtherShop(shopId, pageDataBeans.get(i).getShops().get(k).getShopName(), false);
                                    groupP = i;
                                    childP = k;
                                }
                            }
                        }
                    }
                    getAdapter().notifyDataSetChanged();
                    orderView.expandGroup(pageDataBeans.size());
                }
            }

            @Override
            public void onNeedLogin(String msg) {

            }

            @Override
            public void onFail(Response_Base response_baseModel) {
                orderView.showToastDialog(response_baseModel.getMsg());
            }

        });
    }


}
